package com.weijianhuawen.wjblog.admin.event.subscriber;

import com.weijianhuawen.wjblog.admin.event.ReadArticleEvent;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.StatisticsArticlePvMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-01 13:47
 */

@Component
@Slf4j
public class ReadArticleSubscriber implements ApplicationListener<ReadArticleEvent> {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private StatisticsArticlePvMapper statisticsArticlePvMapper;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(ReadArticleEvent event) {
        // 处理接受到的数据
        Long articleId = event.getArticliId();

        // 获取当前线程名称
        String threadName = Thread.currentThread().getName();

        log.info("【事件监听者】 threadName: {}", threadName);

        // 将订阅文章阅读量+1
        articleMapper.increaseReadNum(articleId);
        log.info("【事件监听者】 文章阅读事件消费成功，articleId: {}", articleId);
        // 当日pv量+1
        LocalDate nowDate = LocalDate.now();
        statisticsArticlePvMapper.increasePvCount(nowDate);
        log.info("【事件监听者】 完成当日pv量+1，日期：{}", nowDate);
    }
}
